package com.platform.omp.OperationalModule.customerInjure.controller;

import com.platform.omp.OperationalModule.customerInjure.entity.DCustomerinjureQuery;
import com.platform.omp.OperationalModule.customerInjure.entity.HrOrgDept;
import com.platform.omp.OperationalModule.customerInjure.dao.HrOrgDeptDao;
import com.platform.omp.OperationalModule.customerInjure.entity.DCustomerinjure;
import com.platform.omp.OperationalModule.customerInjure.service.CustomerInjureService;
import com.platform.omp.common.exceptionhandler.MyException;
import com.platform.omp.common.utils.PageTableRequest;
import com.platform.omp.common.utils.Result;
import com.platform.omp.log.aop.MyLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 类描述： 客伤管理
 *
 * @ClassName CustomerInjureController
 * @Author liucl
 * @Date 2022/3/15
 */
@Controller
@RequestMapping("/customerInjure")
@Api(tags = "客伤:客伤管理")
public class CustomerInjureController {

    @Autowired
    private CustomerInjureService customerInjureService;

    @GetMapping("/add")
    @ApiOperation(value = "新增客伤管理页面")
    @PreAuthorize("hasAnyAuthority('customerInjure:add')")
    public String addJob(Model model) {
        model.addAttribute("customerInjure", new DCustomerinjure());
        return "/operation/customerInjure/customerInjure-add";
    }

    @PostMapping()
    @ResponseBody
    @ApiOperation(value = "新增客伤信息")
    @PreAuthorize("hasAnyAuthority('customerInjure:save')")
    @MyLog("新增客伤信息")
    public Result<DCustomerinjure> saveInsertInjure(@Valid @RequestBody DCustomerinjure dCustomerinjure) {

        /*if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals( deptService.checkDeptNameUnique(dept))) {
            return Result.error().message("新增岗位'" + dept.getDeptName() + "'失败，岗位名称已存在");
        }
        int i = deptService.insertDept(dept);*/
        int i = customerInjureService.insertInjure(dCustomerinjure);
        return Result.judge(i, "添加");
    }

    @PostMapping("/editDetail")
    @ResponseBody
    @ApiOperation(value = "编辑客伤信息")
    @PreAuthorize("hasAnyAuthority('customerInjure:edit')")
    @MyLog("修改客伤信息")
    public Result<DCustomerinjure> updateInsertInjure(@RequestBody DCustomerinjure dCustomerinjure) {

        /*if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals( deptService.checkDeptNameUnique(dept))) {
            return Result.error().message("新增岗位'" + dept.getDeptName() + "'失败，岗位名称已存在");
        }
        int i = deptService.insertDept(dept);*/
        int i = customerInjureService.updateInjure(dCustomerinjure);
        return Result.judge(i, "修改");
    }

    @DeleteMapping("delete")
    @ResponseBody
    @ApiOperation(value = "删除客伤信息")
    @PreAuthorize("hasAnyAuthority('customerInjure:del')")
    public Result<DCustomerinjure> deleteInsertInjure(String ids) {
        try {
            customerInjureService.deleteInsertInjure(ids);
            return Result.ok().message("删除成功");
        } catch (MyException e) {
            return Result.error().message(e.getMsg()).code(e.getCode());
        }
    }

    @PostMapping("/queryList")
    @ResponseBody
    @ApiOperation(value = "分页条件查询客伤列表")
    @PreAuthorize("hasAnyAuthority('customerInjure:queryList')")
    public Result queryDCustomerinjureList( PageTableRequest pageTableRequest,@RequestBody DCustomerinjureQuery injure) {
        pageTableRequest.countOffset();
        // todo 根据当前登录人匹配,其对应车站的信息
        Result result = customerInjureService.queryInjurePage(pageTableRequest.getOffset(), pageTableRequest.getLimit(), injure);
        return result;

    }

    @PostMapping("queryId")
    @ResponseBody
    @ApiOperation(value = "查看客伤信息")
    @PreAuthorize("hasAnyAuthority('customerInjure:queryId')")
    public Result queryCustomerInjureId( long id) {

        // todo 根据当前登录人匹配,其对应车站的信息
        Result result = customerInjureService.queryCustomerInjureId(id);
        return result;

    }


    //HrOrgDeptDao
    @GetMapping("queryhrOrgDeptsList")
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public Result<HrOrgDept> queryhrOrgDeptsList() {
        try {
            List<HrOrgDept> hrOrgDepts = customerInjureService.selectHrOrgDeptList();
            List<HrOrgDept> roots = new ArrayList<>();
            for (HrOrgDept dpts : hrOrgDepts) {
                if (dpts.getPDeptId().equals("1")) {
                    roots.add(dpts);
                }
            }
            for (HrOrgDept dept : roots) {
                setChildren(dept, hrOrgDepts);
            }
            //= deptDao.selectHrOrgDeptList();
            System.out.println(roots);
            return Result.ok().data(roots);
        } catch (MyException e) {
            return Result.error().message(e.getMsg()).code(e.getCode());
        }
    }


    public void setChildren(HrOrgDept dept, List<HrOrgDept> hrOrgDepts) {
        List<HrOrgDept> children = new ArrayList<>();
        for (HrOrgDept dept1 : hrOrgDepts) {
            if (Objects.equals(dept.getDeptId(), dept1.getPDeptId())) {
                children.add(dept1);
            }
        }
        dept.setChildren(children);
        if (children.isEmpty()) {
            return;
        }
        children.forEach(e -> setChildren(e, hrOrgDepts));
    }

}
